#! /usr/bin/python

import wx
import feedparser
import MySQLdb




#class AGDialog(wx.Dialog):
#    def __init__(self,parent,id,title):
#        wx.Dialog.__init__(self,parent,id,title)

#        vbox= wx.BoxSizer(wx.VERTICAL)
#        stline = 

class AGAbout(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self,parent, id, title)
        
        vbox = wx.BoxSizer(wx.VERTICAL)
        stline = wx.StaticText(self, 11, 'AniGrid v0')

        vbox.Add(stline, 1, wx.ALIGN_CENTER|wx.TOP, 40)
        line = wx.StaticLine(self, -1, (25, 50), (300,1))
        vbox.Add(line,1,wx.EXPAND)

        stline = wx.StaticText(self, 11, 'can do shit')
        vbox.Add(stline, 1, wx.ALIGN_CENTER, 20)
        sizer =  self.CreateButtonSizer(wx.OK)
        vbox.Add(sizer, 0, wx.ALIGN_CENTER)
        self.SetSizer(vbox)
        self.Bind(wx.EVT_BUTTON, self.onOK, id=wx.ID_OK)

    def onOK(self, event):
        self.Close()

### class PWatch, Main tab , for watching your stuff

class PWatch(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        t = wx.StaticText(self, -1, "This is a PageOne object", (20,20))


### class PRSS, rss reader tab

class PRSS(wx.Panel):
#    tree = ''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        
        framebox = wx.BoxSizer(wx.VERTICAL)        #Top frame box
        splitbox = wx.BoxSizer(wx.HORIZONTAL)      #left side and right side
        treebox = wx.BoxSizer(wx.VERTICAL)         # contains only the tree for sizing
        buttonbox = wx.BoxSizer(wx.HORIZONTAL)     # will hold the buttons on bottom 

        treepanel = wx.Panel(self, -1)             # left side tree view
        infopanel = wx.Panel(self, -1)             # right side info window

        PRSS.tree = wx.TreeCtrl(treepanel, 1, wx.DefaultPosition, (-1,-1), wx.TR_HIDE_ROOT|wx.TR_HAS_BUTTONS)

        treefont = wx.Font(10,wx.DEFAULT,wx.DEFAULT,wx.DEFAULT)
        PRSS.tree.SetFont(treefont)

        PRSS.root = PRSS.tree.AddRoot('Feeds')
        feeds = []

        dbc=AniGrid.db.cursor()
        dbc.execute("SELECT id,name,url from rss_feeds")

        for result in dbc.fetchall():
            feeds.append(PRSS.tree.AppendItem(PRSS.root, result[1]))
            feed = feedparser.parse( result[2] )
            for i in feed.entries:
                PRSS.tree.AppendItem(feeds[len(feeds)-1], i.title )

        for f in feeds:
            PRSS.tree.SetItemBold(f,True)

        # feed 

        PRSS.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=1)
        self.display = wx.StaticText(infopanel, -1, '',(20,30), style=wx.ALIGN_CENTRE)

        # buttonbox 
        
        buttonbox.Add(wx.Button(self,201,"Refresh"),1,wx.EXPAND)
        buttonbox.Add(wx.Button(self,202,"Add"),1,wx.EXPAND)
        buttonbox.Add(wx.Button(self,203,"Edit"),1,wx.EXPAND)
        buttonbox.Add(wx.Button(self,204,"Delete"),1,wx.EXPAND)

#        self.Bind(wx.EVT_BUTTON, self.OnRefresh, id=201)
        self.Bind(wx.EVT_BUTTON, self.OnAdd, id=202)
#        self.Bind(wx.EVT_BUTTON, self.OnEdit, id=203)
#        self.Bind(wx.EVT_BUTTON, self.OnDelete, id=204)

        # frame layout

        treebox.Add(PRSS.tree, 1, wx.EXPAND)
        splitbox.Add(treepanel, 2, wx.EXPAND | wx.ALL,2)
        splitbox.Add(infopanel, 1, wx.EXPAND | wx.ALL,2)
        treepanel.SetSizer(treebox)

        framebox.Add(splitbox,1,wx.EXPAND)
        framebox.Add(buttonbox,0,wx.ALIGN_BOTTOM | wx.EXPAND | wx.ALL, 3)
        self.SetSizer(framebox)


    def OnSelChanged(self, event):
        item =  event.GetItem()
        self.display.SetLabel(self.tree.GetItemText(item))

    def OnAdd(self,event):
        dlg = RSSadd(self, -1,'Add Rss Feed')

class RSSadd(wx.Dialog):
    def __init__(self,parent,id,title):
        wx.Dialog.__init__(self, parent, id , title)
                
        self.SetSize((500,170))
        wx.StaticText(self, -1, 'Name:',(30,20))
        wx.StaticText(self, -1, 'URL:',(30,50))
        self.name = wx.TextCtrl(self, -1, '', (100,17),(350,30))
        self.url = wx.TextCtrl(self, -1, '', (100,47),(350,30))
        self.cb = wx.CheckBox(self, -1, 'Use name from feed', (150, 85))
        
        wx.Button(self, 221, 'Add',(150,120))
        wx.Button(self, 222, 'Cancel',(260,120))

        self.Bind(wx.EVT_BUTTON, self.OnAdd, id=221)
        self.Bind(wx.EVT_BUTTON, self.OnCancel, id=222)
        
        self.Center()
        self.ShowModal()
        self.Destroy()

    def OnAdd(self, event):
        if (self.name.GetValue() == '') or (self.url.GetValue() == '' ):
            dlg = wx.MessageDialog(self, 'Check Fields please!\nSome seem to be empty', 'Error', wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
            
        dbc=AniGrid.db.cursor()
        dbc.execute("insert into rss_feeds (name,url) values (%s,%s)",(self.name.GetValue(),self.url.GetValue()))

        PRSS.tree.AppendItem(PRSS.root, self.name.GetValue())
        PRSS.tree.SetItemBold(PRSS.tree.GetLastChild(PRSS.root),True)
        
        feed = feedparser.parse( self.url.GetValue() )
        for i in feed.entries:
            PRSS.tree.AppendItem(PRSS.tree.GetLastChild(PRSS.root), i.title )
                  
        self.Close()


    def OnCancel(self, event):
        self.Close()



class AGFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        ### Menu start
        menubar = wx.MenuBar()
        file = wx.Menu()
        edit = wx.Menu()
        help = wx.Menu()
        file.Append(101, '&Open', 'Open a new document')
        file.Append(102, '&Save', 'Save the document')
        file.AppendSeparator()
        quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application')
    #    quit.SetBitmap(wx.Image('stock_exit-16.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap())
        file.AppendItem(quit)
        edit.Append(201, 'check item1', '', wx.ITEM_CHECK)
        edit.Append(202, 'check item2', kind=wx.ITEM_CHECK)
        submenu = wx.Menu()
        submenu.Append(301, 'radio item1', kind=wx.ITEM_RADIO)
        submenu.Append(302, 'radio item2', kind=wx.ITEM_RADIO)
        submenu.Append(303, 'radio item3', kind=wx.ITEM_RADIO)
        edit.AppendMenu(203, 'submenu', submenu)
        help.Append(401, '&About', 'Provides general info')

        menubar.Append(file, '&File')
        menubar.Append(edit, '&Edit')
        menubar.Append(help, '&Help')
        
        ### menu binds

        self.Bind(wx.EVT_MENU, self.OnQuit, id=105)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=401)
        
        ### Statusbar

        self.statusbar = self.CreateStatusBar()

        self.SetMenuBar(menubar)

        ### Notebook Tabs

        nbpanel = wx.Panel(self)
        nb = wx.Notebook(nbpanel)

        page1 = PWatch(nb)
        page2 = PRSS(nb)

        nb.AddPage(page1, "Watch")
        nb.AddPage(page2, "RSS")

        nbsizer = wx.BoxSizer()
        nbsizer.Add(nb, 1, wx.EXPAND)
        nbpanel.SetSizer(nbsizer)

        ### Notebook Tabs End
        self.Centre()

        ### def's 
        
    def OnQuit(self, event):
        self.Close()
    
    def OnAbout(self, event):
        aboutdia = AGAbout(self, -1,'')
        aboutdia.ShowModal()
        aboutdia.Destroy()

        ### menu end
        
class AniGrid(wx.App):
    def OnInit(self):
        frame = AGFrame(None, -1, 'AniGrid')
        frame.SetSize(wx.Size(1000,800))
        frame.Center()
        frame.Show(True)
        return True
    
    db=MySQLdb.connect(user="anigrid",passwd="anigrid",db="anigrid")
    


app = AniGrid(0)
app.MainLoop()
            
